package com.test;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.connection.DataConnection;

public class ProductCheck extends ListActivity {
	
	static String proPicture, proName, proPrice, proIntroduction;
	
	DataConnection data = new DataConnection(this);
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		data.open();
		Cursor c = data.queryProductInfo();
		String[] from = {"picture", "productName", "price", "introduction"};
		int[] to = { R.id.image, R.id.text, R.id.price, R.id.intro};
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,	R.layout.productcheck, c, from, to);

		ListView listView = getListView();
		listView.setAdapter(adapter);
		
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);

		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapter, View arg1,
					int arg2, long arg3) {
				final long temp = arg3;
				builder.setMessage("do you really want to delete?")
						.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface arg0, int arg1) {
									
									data.deleteProductInfo(temp);
									
									Cursor c = data.queryProductInfo();
									String[] from = {"picture", "productName", "price", "introduction"};
									int[] to = { R.id.image, R.id.text, R.id.price, R.id.intro};
									SimpleCursorAdapter adapter = new SimpleCursorAdapter(
											getApplicationContext(),
											R.layout.productcheck, c, from, to);
									ListView listView = getListView();
									listView.setAdapter(adapter);

								}

							})
						.setNegativeButton("No",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}

								});
				AlertDialog ad = builder.create();
				ad.show();

			}

		});
		

	}

}
